﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EasyTemplates
{
    class TypeGeneratorCache : ITemplateCompiler
    {
        string oldTemplateBody;
        TemplateSignature oldTemplateSignature;
        Type oldGeneratedType;

        public Type GenerateType()
        {
            if ( oldGeneratedType == null ||
                 oldTemplateBody != Template.TemplateBody ||
                 !oldTemplateSignature.Matches( 
                        new TemplateSignature(Template)
                 )
               )
            {
                oldTemplateBody = Template.TemplateBody;
                oldTemplateSignature = new TemplateSignature(Template);
                TemplateCompiler compiler = new TemplateCompiler();
                compiler.Template = Template;
                oldGeneratedType = compiler.GenerateType();
            }

            return oldGeneratedType;
        }

        public Template Template
        {
            get;
            set;
        }
    }
}
